package com.jeesit.cloud.spot.business.impl.Impl;

import com.jeesit.cloud.dto.JSONResult;
import com.jeesit.cloud.dto.MaterialCatalogDto;
import com.jeesit.cloud.dto.MaterialSpotDto;
import com.jeesit.cloud.dto.MaterialTopDto;
import com.jeesit.cloud.model.*;
import com.jeesit.cloud.spot.business.impl.IMaterialBiz;
import com.jeesit.cloud.spot.service.IMaterialCatalogService;
import com.jeesit.cloud.spot.service.IMaterialProductService;
import com.jeesit.cloud.spot.service.IMaterialSpotService;
import com.jeesit.cloud.spot.service.IMaterialTopService;
import com.jeesit.cloud.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class MaterialBizImpl implements IMaterialBiz {

    @Autowired
    private IMaterialTopService materialTopService;

    @Autowired
    private IMaterialCatalogService materialCatalogService;

    @Autowired
    private IMaterialSpotService materialSpotService;

    @Autowired
    private IMaterialProductService materialProductService;


    /**
     * 添加分类、品类
     * @param user
     * @param id
     * @param entity
     * @return
     */
    @Override
    @Transactional
    public JSONResult<MaterialTop> saveDataMaterialTop(User user, Long id, MaterialTopDto entity) {
        MaterialTop materialTop = new MaterialTop();
        if(entity.getUuid()==null){//id为空是添加分类，不为空是添加品类
            MaterialTopDto materialTopDto = new MaterialTopDto();
            materialTopDto.setNames(entity.getName());
            List<MaterialTop> list = materialTopService.list(1, materialTopDto);
            Assert.isTrue(list.size()==0,"物料分类已存在，请核对后重新输入");
            materialTop.setAddTime(new Date());
            materialTop.setAddUserUuid(user.getId());
            materialTop.setDeleted(false);
            materialTop.setOrgUuid(entity.getOrgUuid());
            materialTop.setName(entity.getName());
            materialTopService.saveOrUpdate(materialTop);
            entity.setUuid(materialTop.getUuid());
        }

        if(entity.getCatalogName()!=null&&entity.getCatalogName().length>.0){
            List<MaterialCatalog> materialCatalogs = new ArrayList<>();
            for(String s:entity.getCatalogName()){
                MaterialCatalogDto materialCatalogDto = new MaterialCatalogDto();
                materialCatalogDto.setMaterialTopUuid(entity.getUuid());
                materialCatalogDto.setName(s);
                List<MaterialCatalog> list = materialCatalogService.list(1, materialCatalogDto);
                Assert.isTrue(list.size()==0,"物料品类已存在，请核对后重新输入");
                MaterialCatalog materialCatalog = new MaterialCatalog();
                materialCatalog.setDeleted(false);
                materialCatalog.setAddTime(new Date());
                materialCatalog.setAddUserName(user.getNickName());
                materialCatalog.setAddUserUuid(user.getId());
                materialCatalog.setMaterialTopUuid(entity.getUuid());
                materialCatalog.setMaterialTopName(entity.getName());
                materialCatalog.setName(s);
                materialCatalogs.add(materialCatalog);
            }
            materialCatalogService.saveOrUpdateBatch(materialCatalogs);
        }
        JSONResult<MaterialTop> res = new JSONResult<>();
        res.setMessage("添加成功");
        res.setSuccess(true);
        res.setData(materialTop);
        return res;
    }

    /**
     * 添加品种
     * @param user
     * @param id
     * @param entity
     * @return
     */
    @Override
    @Transactional
    public JSONResult<MaterialSpot> saveDataMaterialSpot(User user, Long id, MaterialSpotDto entity) {
        MaterialSpot it = new MaterialSpot();
        MaterialSpotDto materialSpotDto = new MaterialSpotDto();
        materialSpotDto.setNames(entity.getName());
        materialSpotDto.setCatalogUuid(entity.getCatalogUuid());
        List<MaterialSpot> list = materialSpotService.list(1, materialSpotDto);
        if(id==null){
            Assert.isTrue(list.size()==0,"品名已存在，请核对后重新输入");
            it.setDeleted(false);
            it.setAddTime(new Date());
            it.setAddUserName(user.getNickName());
            it.setAddUserUuid(user.getId());
        } else {
            Assert.isTrue(list.size()==0||list.get(0).getUuid().equals(id),"品名已存在，请核对后重新输入");
            it = materialSpotService.getById(entity.getUuid());
            it.setModTime(new Date());
            it.setModUserUuid(user.getId());
            it.setModUserName(user.getNickName());
        }
        it.setMaterialTopUuid(entity.getMaterialTopUuid());
        it.setUuid(entity.getUuid());
        it.setName(entity.getName());
        it.setCatalogUuid(entity.getCatalogUuid());
        it.setRemark(entity.getRemark());
        it.setIsUse(entity.getIsUse());
        materialSpotService.saveOrUpdate(it);
        String productNames ="";
        if(entity.getProductUuid()!=null&&entity.getProductUuid().length>0){
            List<MaterialProduct> materialProducts = new ArrayList<>();
            materialProductService.deltedDate(it.getUuid());//先删除原有的关系
            Long[] productId = entity.getProductUuid();
            String[] productName = entity.getProductName();
            String[] product = entity.getProduct();
            for(int i=0;i<productId.length;i++){
                MaterialProduct materialProduct = new MaterialProduct();
                materialProduct.setDeleted(false);
                materialProduct.setAddTime(new Date());
                materialProduct.setAddUserUuid(user.getId());
                materialProduct.setAddUserName(user.getNickName());
                materialProduct.setMaterialSpotUuid(it.getUuid());
                materialProduct.setMaterialSpotName(it.getName());
                materialProduct.setProductUuid(productId[i]);
                materialProduct.setProductName(productName[i]);
                materialProduct.setProduct(product[i]);
                materialProducts.add(materialProduct);
                if(StringUtil.isBlank(productNames)){
                    productNames= productNames+product[i]+"（"+productName[i]+")";
                }else {
                    productNames= productNames+","+product[i]+"（"+productName[i]+")";
                }
            }
            if(!StringUtil.isBlank(productNames)){
                it.setProductNames(productNames.trim());
            }
            materialSpotService.saveOrUpdate(it);
            materialProductService.saveOrUpdateBatch(materialProducts);
        }
        JSONResult<MaterialSpot> res = new JSONResult<>();
        res.setMessage("添加成功");
        res.setSuccess(true);
        res.setData(it);
        return res;
    }
}
